/*
 * Copyright (C) 2004, Velocityme Partnership
 * ContactableActions.java
 *
 * Created on 28 September 2004, 07:45
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.entity.ContactDetail;
import com.velocityme.entity.Contactable;
import com.velocityme.session.ContactDetailLocal;
import com.velocityme.session.KeyLocal;
import com.velocityme.www.actionforms.nodeactionforms.ContactableActionForm;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author  Robert
 */
public class ContactableActions extends NodeActions {

    DecimalFormat m_decimalFormat = new DecimalFormat("000");

    /** Creates a new instance of ContactableActions */
    public ContactableActions() {
    }

    protected void createContactDetails(KeyLocal key,
                                        ContactableActionForm contactableValueAF) {
        contactableValueAF.setContactDetailTypes(lookupContactDetailBean().findAllContactDetailTypes(key));
    }

    protected void editContactDetails(KeyLocal key,
                                      Contactable contactable,
                                      ContactableActionForm contactableValueAF) {
        contactableValueAF.setContactDetailTypes(lookupContactDetailBean().findAllContactDetailTypes(key));
        Collection<ContactDetail> contactDetailValues = contactable.getContactDetails();
        for (ContactDetail contactDetail: contactDetailValues) {
            String number = m_decimalFormat.format(contactDetail.getContactDetailType().getSequenceNumber());
            contactableValueAF.setContactDetailValue(number, contactDetail.getValue());
            contactableValueAF.setContactDetailId(number, contactDetail.getId());
        }
    }

    private ContactDetailLocal lookupContactDetailBean() {
        try {
            Context c = new InitialContext();
            return (ContactDetailLocal) c.lookup("java:comp/env/ContactDetailBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
